package massim.jason.acao;

import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.NumberTerm;
import jason.asSyntax.ObjectTermImpl;
import jason.asSyntax.Term;

public class FindDirection extends DefaultInternalAction {
	private static final long serialVersionUID = -4595797083673870714L;

	@Override
	public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
		Term x = args[0];
		Term y = args[1];
		Term newX = args[2];
		Term newY = args[3];
		Term direction = args[4];
		
		// Unfold terms
		double vx = ((NumberTerm)x).solve();
		double vy = ((NumberTerm)y).solve();
		double vNewX = ((NumberTerm)newX).solve();
		double vNewY = ((NumberTerm)newY).solve();
		
		Direction vDirection = Direction.getDirection(vx, vy, vNewX, vNewY);
		
		// Update output terms
		un.unifies(direction, new ObjectTermImpl(vDirection));
		
		// everything ok, so returns true
		return true;
	}
}
